/**
 * 智慧小区管理系统 - 主题配置
 * @author Wu.Liang
 * @date 2025-01-30
 */

export interface ThemeConfig {
  id: string
  name: string
  description: string
  icon: string
  colors: {
    primary: string
    success: string
    warning: string
    error: string
    info: string
    background: string
    surface: string
    text: string
    textSecondary: string
    border: string
  }
  isDark: boolean
}

/**
 * 预设主题配置
 */
export const THEMES: ThemeConfig[] = [
  {
    id: 'default',
    name: '默认主题',
    description: '经典蓝色主题，适合大多数场景',
    icon: '🌊',
    colors: {
      primary: '#409EFF',
      success: '#67C23A',
      warning: '#E6A23C',
      error: '#F56C6C',
      info: '#909399',
      background: '#f5f5f5',
      surface: '#ffffff',
      text: '#303133',
      textSecondary: '#606266',
      border: '#DCDFE6'
    },
    isDark: false
  },
  {
    id: 'ocean',
    name: '海洋主题',
    description: '清新海洋蓝，给人宁静舒适的感觉',
    icon: '🌊',
    colors: {
      primary: '#1890ff',
      success: '#52c41a',
      warning: '#faad14',
      error: '#ff4d4f',
      info: '#13c2c2',
      background: '#f0f8ff',
      surface: '#ffffff',
      text: '#262626',
      textSecondary: '#8c8c8c',
      border: '#d9d9d9'
    },
    isDark: false
  },
  {
    id: 'forest',
    name: '森林主题',
    description: '自然绿色，环保清新',
    icon: '🌲',
    colors: {
      primary: '#52c41a',
      success: '#73d13d',
      warning: '#ffc53d',
      error: '#ff7875',
      info: '#36cfc9',
      background: '#f6ffed',
      surface: '#ffffff',
      text: '#262626',
      textSecondary: '#8c8c8c',
      border: '#b7eb8f'
    },
    isDark: false
  },
  {
    id: 'sunset',
    name: '日落主题',
    description: '温暖橙色，充满活力',
    icon: '🌅',
    colors: {
      primary: '#fa8c16',
      success: '#52c41a',
      warning: '#faad14',
      error: '#ff4d4f',
      info: '#13c2c2',
      background: '#fff7e6',
      surface: '#ffffff',
      text: '#262626',
      textSecondary: '#8c8c8c',
      border: '#ffd591'
    },
    isDark: false
  },
  {
    id: 'purple',
    name: '紫色主题',
    description: '优雅紫色，高端大气',
    icon: '💜',
    colors: {
      primary: '#722ed1',
      success: '#52c41a',
      warning: '#faad14',
      error: '#ff4d4f',
      info: '#13c2c2',
      background: '#f9f0ff',
      surface: '#ffffff',
      text: '#262626',
      textSecondary: '#8c8c8c',
      border: '#d3adf7'
    },
    isDark: false
  },
  {
    id: 'dark',
    name: '深色主题',
    description: '护眼深色，适合夜间使用',
    icon: '🌙',
    colors: {
      primary: '#1890ff',
      success: '#52c41a',
      warning: '#faad14',
      error: '#ff4d4f',
      info: '#13c2c2',
      background: '#141414',
      surface: '#1f1f1f',
      text: '#ffffff',
      textSecondary: '#a6a6a6',
      border: '#303030'
    },
    isDark: true
  },
  {
    id: 'midnight',
    name: '午夜主题',
    description: '深邃黑色，极致护眼',
    icon: '🌃',
    colors: {
      primary: '#177ddc',
      success: '#49aa19',
      warning: '#d89614',
      error: '#d32029',
      info: '#08979c',
      background: '#000000',
      surface: '#141414',
      text: '#ffffff',
      textSecondary: '#8c8c8c',
      border: '#1f1f1f'
    },
    isDark: true
  },
  {
    id: 'coffee',
    name: '咖啡主题',
    description: '温暖棕色，如咖啡般醇厚',
    icon: '☕',
    colors: {
      primary: '#8b4513',
      success: '#52c41a',
      warning: '#faad14',
      error: '#ff4d4f',
      info: '#13c2c2',
      background: '#faf6f1',
      surface: '#ffffff',
      text: '#2c1810',
      textSecondary: '#8c8c8c',
      border: '#d2b48c'
    },
    isDark: false
  }
]

/**
 * 获取主题配置
 */
export function getThemeConfig(themeId: string): ThemeConfig {
  const theme = THEMES.find(theme => theme.id === themeId)
  return (theme ?? THEMES[0]) as ThemeConfig
}

/**
 * 获取默认主题
 */
export function getDefaultTheme(): ThemeConfig {
  return THEMES[0] as ThemeConfig
}

/**
 * 应用主题到CSS变量
 */
export function applyTheme(theme: ThemeConfig): void {
  const root = document.documentElement
  
  // 设置主题色彩
  root.style.setProperty('--primary-color', theme.colors.primary)
  root.style.setProperty('--success-color', theme.colors.success)
  root.style.setProperty('--warning-color', theme.colors.warning)
  root.style.setProperty('--error-color', theme.colors.error)
  root.style.setProperty('--info-color', theme.colors.info)
  
  // 设置背景和表面色彩
  root.style.setProperty('--background-color', theme.colors.background)
  root.style.setProperty('--surface-color', theme.colors.surface)
  
  // 设置文字色彩
  root.style.setProperty('--text-color', theme.colors.text)
  root.style.setProperty('--text-secondary-color', theme.colors.textSecondary)
  
  // 设置边框色彩
  root.style.setProperty('--border-color', theme.colors.border)
  
  // 设置侧边栏色彩
  if (theme.isDark) {
    root.style.setProperty('--sidebar-background', '#1f1f1f')
    root.style.setProperty('--sidebar-text', '#a6a6a6')
    root.style.setProperty('--sidebar-active-text', theme.colors.primary)
    root.style.setProperty('--sidebar-border', '#2f2f2f')
    root.style.setProperty('--sidebar-header-background', '#2b2b2b')
    root.style.setProperty('--sidebar-hover-background', '#2a2a2a')
    root.style.setProperty('--sidebar-active-background', theme.colors.primary)
  } else {
    root.style.setProperty('--sidebar-background', '#304156')
    root.style.setProperty('--sidebar-text', '#bfcbd9')
    root.style.setProperty('--sidebar-active-text', theme.colors.primary)
    root.style.setProperty('--sidebar-border', '#263445')
    root.style.setProperty('--sidebar-header-background', '#2b3a4a')
    root.style.setProperty('--sidebar-hover-background', '#263445')
    root.style.setProperty('--sidebar-active-background', theme.colors.primary)
  }
  
  // 设置主题类名
  root.className = root.className.replace(/theme-\w+/g, '')
  root.classList.add(`theme-${theme.id}`)
  
  // 设置深色模式
  if (theme.isDark) {
    root.classList.add('dark')
  } else {
    root.classList.remove('dark')
  }
  
  // 更新HTML的data-theme属性
  document.documentElement.setAttribute('data-theme', theme.id)
}

/**
 * 保存主题到本地存储
 */
export function saveTheme(themeId: string): void {
  localStorage.setItem('smart_community_theme', themeId)
}

/**
 * 从本地存储加载主题
 */
export function loadTheme(): string {
  return localStorage.getItem('smart_community_theme') || 'default'
} 